import { useEffect } from 'react';
import { useUserStore, useColorStore, useRecommendationStore } from './index';

/**
 * 应用初始化 Hook
 * 在应用启动时初始化所有 stores
 */
export const useAppInitialization = () => {
  const initializeUser = useUserStore(state => state.initialize);
  const isUserLoading = useUserStore(state => state.isLoading);
  
  useEffect(() => {
    // 初始化用户状态
    initializeUser();
  }, [initializeUser]);

  return {
    isLoading: isUserLoading
  };
};

/**
 * 错误处理 Hook
 * 统一处理所有 stores 的错误状态
 */
export const useGlobalError = () => {
  const userError = useUserStore(state => state.error);
  const colorError = useColorStore(state => state.error);
  const recommendationError = useRecommendationStore(state => state.error);
  
  const clearUserError = useUserStore(state => state.clearError);
  const clearColorError = useColorStore(state => state.clearError);
  const clearRecommendationError = useRecommendationStore(state => state.clearError);

  const hasError = !!(userError || colorError || recommendationError);
  const error = userError || colorError || recommendationError;

  const clearError = () => {
    clearUserError();
    clearColorError();
    clearRecommendationError();
  };

  return {
    hasError,
    error,
    clearError
  };
};

/**
 * 加载状态 Hook
 * 统一管理所有 stores 的加载状态
 */
export const useGlobalLoading = () => {
  const userLoading = useUserStore(state => state.isLoading);
  const colorLoading = useColorStore(state => state.isLoading);
  const recommendationLoading = useRecommendationStore(state => state.isLoading);

  return {
    isLoading: userLoading || colorLoading || recommendationLoading,
    userLoading,
    colorLoading,
    recommendationLoading
  };
};

/**
 * 主题管理 Hook
 * 基于用户偏好管理应用主题
 */
export const useTheme = () => {
  const preferences = useUserStore(state => state.preferences);
  const updatePreferences = useUserStore(state => state.updatePreferences);

  const setTheme = (theme: 'light' | 'dark' | 'auto') => {
    updatePreferences({ theme });
  };

  // 根据系统偏好或用户设置确定当前主题
  const getCurrentTheme = (): 'light' | 'dark' => {
    if (preferences.theme === 'auto') {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
    return preferences.theme === 'dark' ? 'dark' : 'light';
  };

  return {
    theme: preferences.theme,
    currentTheme: getCurrentTheme(),
    setTheme
  };
};

/**
 * 快速操作 Hook
 * 提供常用的快速操作方法
 */
export const useQuickActions = () => {
  const currentColor = useColorStore(state => state.currentColor);
  const addSelectedColor = useColorStore(state => state.addSelectedColor);
  const generateRecommendations = useRecommendationStore(state => state.generateRecommendations);
  const generateRandomPalette = useColorStore(state => state.generateRandomPalette);

  const quickAddCurrentColor = () => {
    addSelectedColor(currentColor);
  };

  const quickGenerateComplementary = async () => {
    await generateRecommendations(currentColor, 'complementary');
  };

  const quickGenerateAnalogous = async () => {
    await generateRecommendations(currentColor, 'analogous');
  };

  const quickGenerateTriadic = async () => {
    await generateRecommendations(currentColor, 'triadic');
  };

  const quickGenerateRandom = () => {
    generateRandomPalette(5);
  };

  return {
    quickAddCurrentColor,
    quickGenerateComplementary,
    quickGenerateAnalogous,
    quickGenerateTriadic,
    quickGenerateRandom
  };
};

/**
 * 数据同步 Hook
 * 处理本地存储与 stores 之间的数据同步
 */
export const useDataSync = () => {
  const user = useUserStore(state => state.user);
  const selectedColors = useColorStore(state => state.selectedColors);
  const activePalette = useColorStore(state => state.activePalette);

  // 自动保存功能
  useEffect(() => {
    if (user?.preferences.autoSave && activePalette && selectedColors.length > 0) {
      // 定时保存逻辑可以在这里实现
      const timer = setTimeout(() => {
        // 自动保存当前调色板
        console.log('Auto-saving palette...');
      }, 5000); // 5秒后自动保存

      return () => clearTimeout(timer);
    }
  }, [user, selectedColors, activePalette]);

  return {
    isSyncEnabled: user?.preferences.autoSave || false
  };
};